﻿using _WebShared.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KPIS.GERP.GFMS.DAL
{
    public class ConvertNullable
    {
        public Nullable<int> ConvertNullInt(Object o)
        {
            Nullable<int> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<int>)Convert.ToInt32(o)));
            return result;
        }

        public Nullable<double> ConvertNullDouble(Object o)
        {
            Nullable<double> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<double>)Convert.ToDouble(o)));
            return result;
        }

        public Nullable<DateTime> ConvertNullDateTime(Object o)
        {
            Nullable<DateTime> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<DateTime>)Convert.ToDateTime(o)));
            return result;
        }

        public Nullable<TimeSpan> ConvertNullTimeSpan(Object o)
        {
            Nullable<TimeSpan> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<TimeSpan>)Convert.ToDateTime(o).TimeOfDay));
            return result;
        }

        public Nullable<decimal> ConvertNullDecimal(Object o)
        {
            Nullable<decimal> result = (o == null ? null : (o.ToString().Length == 0 ? null : (Nullable<decimal>)Convert.ToDecimal(o)));
            return result;
        }

        private bool IsAValidNumber(string strNumberText)
        {
            // There can be only one point and the others must be 0-9
            int i;
            int nPointCount = 0;
            string strChar;
            bool bOK = true;

            for (i = 0; i < strNumberText.Length; i++)
            {
                strChar = strNumberText.Substring(i, 1);

                if (strChar == ".")
                    nPointCount++;

                if (nPointCount > 1 || strChar != "." && (int.Parse(strChar) < 0 || int.Parse(strChar) > 9))
                {
                    bOK = false;
                    break;
                }
            }

            return bOK;
        }

        private string EachNumberPositionThaiText(string strReverseNumberText, int nMaxPosition
            , int nCurrentPosition, string[] astrPositionName, string[] astrDigitName)
        {
            int nDigit;
            string strDigitName;
            int nPosition;
            string strPositionName;
            int nPreviousDigit = 0;

            nDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition, 1));

            if (nCurrentPosition < nMaxPosition)
                nPreviousDigit = int.Parse(strReverseNumberText.Substring(nCurrentPosition + 1, 1));
            // notice: lngPosition = (lngCurrentPosition - 1) Mod 6 + 1
            nPosition = (nCurrentPosition - 1) % 6 + 1;

            if (nDigit == 0 && nMaxPosition > 0 || nDigit == 1 && nPosition == 1)
                strDigitName = "";
            else if (nDigit == 1 && (nPosition == 0 || nPosition == 6)
                && nMaxPosition > nCurrentPosition
                && nPreviousDigit > 0)
                strDigitName = "เอ็ด";
            else if (nDigit == 2 && nPosition == 1)
                strDigitName = "ยี่";
            else
                strDigitName = astrDigitName[nDigit];

            if (nDigit == 0 && nPosition != 6)
                strPositionName = "";
            else
                strPositionName = astrPositionName[nPosition];

            return strDigitName + strPositionName;
        }

        public string MoneyTextThai(double dblNumber)
        {
            return MoneyTextThai(dblNumber.ToString(), "บาท", "สตางค์");
        }

        public string MoneyTextThai(decimal dblNumber)
        {
            return MoneyTextThai(dblNumber.ToString(), "บาท", "สตางค์");
        }

        public string MoneyTextThai(string strNumberText)
        {
            return MoneyTextThai(strNumberText, "บาท", "สตางค์");
        }

        public string MoneyTextThai(string strNumberText, string strCurrencyMain, string strCurrencyPercent)
        {
            string strResult = "";
            string[] astrPositionName = { "", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน" };
            string[] astrDigitName = { "ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า" };
            string strReverseBaht;
            string strReverseSatang;
            int nPointPosition;
            int nMaxPosition;
            int nCurrentPosition;

            // simplify the number
            strNumberText = strNumberText.Trim().Replace(",", "");

            while (strNumberText.Length > 1
                && StringX.Left(strNumberText, 1) == "0"
                && StringX.Left(strNumberText, 2) != "0.")
                strNumberText = strNumberText.Substring(1);

            // check number validation
            if (!IsAValidNumber(strNumberText))
                throw new Exception(strNumberText + " ไม่ใช่จำนวนที่ถูกต้อง");

            // separate baht and satang
            nPointPosition = strNumberText.IndexOf(".");

            if (nPointPosition >= 0)
            {
                strReverseBaht = StringX.StrReverse(strNumberText.Substring(0, nPointPosition));
                strReverseSatang
                    = StringX.StrReverse(StringX.Right(double.Parse(strNumberText.Substring(nPointPosition)).ToString(".00"), 2));
            }
            else
            {
                strReverseBaht = StringX.StrReverse(strNumberText);
                strReverseSatang = "00";
            }

            // begin process baht
            nMaxPosition = strReverseBaht.Length - 1;

            for (nCurrentPosition = nMaxPosition; nCurrentPosition >= 0; nCurrentPosition--)
            {
                strResult += EachNumberPositionThaiText(strReverseBaht
                    , nMaxPosition, nCurrentPosition, astrPositionName, astrDigitName);
            }

            if (strResult.Length > 0)
                strResult += strCurrencyMain;
            // end process baht

            // begin process satang
            if (strResult.Length > 0 && strReverseSatang == "00")
            {
                strResult += "ถ้วน";
            }
            else
            {
                if (strResult == "ศูนย์ " + strCurrencyMain)
                    strResult = "";

                nMaxPosition = strReverseSatang.Length - 1;

                for (nCurrentPosition = nMaxPosition; nCurrentPosition >= 0; nCurrentPosition--)
                    strResult += EachNumberPositionThaiText(strReverseSatang
                        , nMaxPosition, nCurrentPosition, astrPositionName, astrDigitName);

                if (strResult.Length > 0)
                    strResult += strCurrencyPercent;
            }
            // end process satang

            return strResult;
        }
    }
}
